package cc.rengu.igas.channel.nucc.core.service.trans;

import cc.rengu.igas.channel.nucc.common.constant.NuccParamConstant;
import cc.rengu.igas.channel.nucc.common.constant.NuccTreeNodeConstant;
import cc.rengu.igas.channel.nucc.facade.bean.CtrlNbInfBean;
import cc.rengu.igas.channel.nucc.facade.request.TransSeqNoApplyRequest;
import cc.rengu.igas.channel.nucc.facade.response.TransSeqNoApplyResponse;
import cc.rengu.igas.channel.nucc.core.realize.NuccService;
import cc.rengu.igas.channel.nucc.core.realize.impl.NuccServiceImpl;
import cc.rengu.oltp.service.base.RadpService;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.utility.util.RedisUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;

import java.util.*;

/**
 * 网联流水号申请
 */
public class EpccTransSeqNoApplyService extends RadpService {
    @Override
    protected int bizInit(String s) {
        return 0;
    }

    @Override
    protected int bizProcess(String s, int i) {
        XmlTreeUtil tree = new XmlTreeUtil();
        String transSeq = null;
        try {
            tree.setXmlTreeStringValue(TreeNodeConstant.INST_ID, s);
            NuccService epccService = new NuccServiceImpl();
            TransSeqNoApplyRequest request = new TransSeqNoApplyRequest();
            tree.setXmlTreeStringValue(NuccTreeNodeConstant.EPCC_ISSR_ID, request.getInstgId());
            //request.setInstgId(NuccParamConstant.EPCC_DEFAULT_INSTID);
            /* 调用网联接口 */
            TransSeqNoApplyResponse response = epccService.transSeqNoApply(request);
            if (NuccParamConstant.EPCC_SUCCESS_CODE.equals(response.getRespCode())) {
                rglog.error("获取流水号失败，respCode=[{}],respDesc=[{}]", response.getRespCode(), response.getRespDesc());
                return -1;
            }
            //更新流水号信息
            List<CtrlNbInfBean> ctrlNbInfList = response.getCtrlNbInfLst();
            //List<String> list = new ArrayList<>();
            StringBuilder stringBuilder = new StringBuilder();
            if (null != ctrlNbInfList && ctrlNbInfList.size() > 0) {
                Iterator<CtrlNbInfBean> itContact = ctrlNbInfList.iterator();
                while (itContact.hasNext()) {
                    CtrlNbInfBean ctrlNbInf = itContact.next();
                    rglog.error("CtrlNbF=[{}],getCtrlNbL[{}]", ctrlNbInf.getCtrlNbF(), ctrlNbInf.getCtrlNbL());
                    String[] str = ctrlNbInf.getCtrlNbL().split("\\|");
                    for (int j = 0; j < str.length; j++) {
                        stringBuilder.append(ctrlNbInf.getCtrlNbF() + str[i]);
                        stringBuilder.append("|");
                    }
                }
            } else {
                rglog.error("未获取到控制位列表");
                return -1;
            }
            transSeq = stringBuilder.substring(0, stringBuilder.length() - 1);
            RedisUtil.onceSet(NuccParamConstant.EPCC_CTRL_NB_CAC, transSeq);

        } catch (Exception e) {
            rglog.error(e.getMessage());
            return -1;
        }
        return 0;
    }

    @Override
    protected int bizTerminate(String s) {
        return 0;
    }
}
